package com.atguigu.edu.controller;

import com.atguigu.edu.entity.Teacher;
import com.atguigu.edu.entity.vo.TeacherQuery;
import com.atguigu.edu.service.TeacherService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-06-29
 */
//swaggerapi描述
@Api(description = "讲师管理-8001")
@RestController
@RequestMapping("/eduservice/teacher")
@Slf4j
public class TeacherController {
    @Autowired
    TeacherService teacherService;

    //swaggerapi方法
    @ApiOperation(value = "查询所有讲师")
    @GetMapping("/findAll")
    public R findAll() {
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "根据id删除讲师")
    @DeleteMapping("{id}")
    //swaggerapi参数
    public R deleteById(@PathVariable(required = true) String id) {
        log.info("接收到的id" + id);
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "讲师列表-无条件+分页")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R selectByPage(@PathVariable(required = true) Long current, @PathVariable(required = true) Long limit) {

        Page<Teacher> page1 = new Page<>(current, limit);
        IPage<Teacher> iPage = teacherService.page(page1, null);
        return R.ok().data("total", iPage.getTotal()).data("recodes", iPage.getRecords());

    }

    /**
     * getMapping 无法获取RequestBody数据？
     *
     * @param current
     * @param limit
     * @param teacherQuery
     * @return
     */
    @ApiOperation(value = "讲师列表-模糊查询+分页")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R selectByCondition(@PathVariable Long current, @PathVariable Long limit, @RequestBody TeacherQuery teacherQuery) {
        log.info("" + teacherQuery);
//        条件为空时 进行无条件分页查询
        if (teacherQuery == null) {
            Page<Teacher> page1 = new Page<>(current, limit);
            IPage<Teacher> iPage = teacherService.page(page1, null);
            return R.ok().data("total", iPage.getTotal()).data("recodes", iPage.getRecords());
        }
//        添加条件
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(teacherQuery.getName())) {
            queryWrapper.like("name", teacherQuery.getName());
        }

        if (null != teacherQuery.getLevel()) {
            queryWrapper.eq("level", teacherQuery.getLevel());
        }
        if (!StringUtils.isEmpty(teacherQuery.getBegin())) {
            queryWrapper.ge("gmt_create", teacherQuery.getBegin());
        }
        if (!StringUtils.isEmpty(teacherQuery.getEnd())) {
            queryWrapper.le("gmt_modified", teacherQuery.getEnd());
        }
        queryWrapper.orderByDesc("gmt_create");
        //添加条件后分页
        Page<Teacher> page1 = new Page<>(current, limit);
        IPage<Teacher> iPage = teacherService.page(page1, queryWrapper);

        return R.ok().data("total", iPage.getTotal()).data("recodes", iPage.getRecords());
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping
    public R insert(@RequestBody() Teacher teacher) {
        try {
            if (teacher.getAvatar() == null || "".equals(teacher.getAvatar())) {
//                默认头像
                teacher.setAvatar("https://guli-file-190513.oss-cn-beijing.aliyuncs.com/avatar/default.jpg");
            }
            boolean save = teacherService.save(teacher);
            if (save) {
                return R.ok();

            } else {
                return R.error().message("添加失败!");
            }
        } catch (Exception e) {
            return R.error().message("添加失败!");
        }

    }

    @ApiOperation(value = "根据id查讲师/修改回显")
    @GetMapping("/{id}")
    public R selectById(@PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        return R.ok().data("teacher", teacher);
    }

    @ApiOperation(value = "修改讲师")
    @PostMapping("/update")
    public R updateById(@RequestBody Teacher teacher) {
        try {
            boolean save = teacherService.updateById(teacher);
            if (save) {
                return R.ok();
            } else {
                return R.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("该名称已存在").data("错误", "已存在");
        }

    }

}

